//
//  ConNearActivityDetailController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/7/22.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConNearActivityDetailController.h"
#import "ActivityDetailCell.h"
#import <Masonry.h>
#import "ActivityFlowContentCell.h"
#import "CustomerTitleCell.h"
#import "CustomerCell.h"
#import "ClubNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "AddCustomerCell.h"
#import <MJRefresh.h>
#import "SZLAccountHelper.h"
#import "ShareTool.h"

@interface ConNearActivityDetailController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UIView *selectView;
@property (nonatomic,weak) IBOutlet UIScrollView *scroll;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UITableView *addTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollCons;

@property (nonatomic,strong) ActivityDetailModel *detailModel;
@property (nonatomic,strong) NSMutableArray *addCustomers;
@property (nonatomic,assign) NSInteger curPage;
@property (weak, nonatomic) IBOutlet UIButton *contentBtn;
@property (weak, nonatomic) IBOutlet UIButton *adderBtn;
@property (weak, nonatomic) IBOutlet UIButton *reportBtn;
@property (weak, nonatomic) IBOutlet UIView *reportView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *reportCons;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *tellField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollBottomCons;

@end

@implementation ConNearActivityDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavi];
    [self setupView];
    [self getNearActivityDetail];
    self.curPage = 1;
    [self getNearActivityAddCustomer];
}
- (void)setNavi
{
    UIButton *editBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [editBtn setTitle:@"分享" forState:UIControlStateNormal];
    editBtn.tag = 0;
    [editBtn setTitleColor:RGBColor(255, 121, 201) forState:UIControlStateNormal];
    editBtn.titleLabel.font = Font(16);
    [editBtn addTarget:self action:@selector(shareClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:editBtn];
}
- (void)shareClick
{
    ShareContentModel *model = [[ShareContentModel alloc]init];
    model.title = self.detailModel.title;
    model.pageUrl = self.detailModel.webActivityUrl;
    model.content = self.detailModel.content;
    model.sharePicUrl = [self.detailModel.activityPictureUrl componentsSeparatedByString:@"#"].firstObject;
    [ShareTool share:model];
}
- (void)setupView
{
    self.reportView.layer.borderWidth = 1;
    self.reportView.layer.borderColor = RGBColor(255, 120, 120).CGColor;
    if (self.frome == SoureceFromeHome) {
        self.reportBtn.hidden = NO;
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 55, 0);
        self.addTableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
        self.reportView.layer.borderWidth = 1;
        self.reportView.layer.borderColor = RGBColor(255, 120, 120).CGColor;
    }else {
        self.reportBtn.hidden = YES;
    }
    self.scroll.contentSize = CGSizeMake(ScreenW*2, 0);
    [self.scroll addSubview:self.tableView];
    [self.scroll addSubview:self.addTableView];
}
- (IBAction)reportClick:(UIButton *)btn
{
    if (self.reportView.hidden) {
        self.reportView.hidden = NO;
    }else {
        [self nearActivityreport];
    }
}
 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.reportView.hidden = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.reportView.hidden = YES;
}
- (IBAction)selectClick:(UIButton *)btn
{
    if (btn == self.contentBtn) {
        self.contentBtn.selected = YES;
        self.adderBtn.selected = NO;
    }else {
        self.contentBtn.selected = NO;
        self.adderBtn.selected = YES;
    }
    self.scroll.contentOffset = CGPointMake(ScreenW*btn.tag, 0);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableView) {
        return 3;
    }
    return self.addCustomers.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        if (section == 0) {
            return 1;
        }else if (section == 1) {
            return 1;
        }
        return 1+self.detailModel.guestList.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            ActivityDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ActivityDetailCell class])];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }else if (indexPath.section == 1) {
            ActivityFlowContentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ActivityFlowContentCell class])];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }
        if (indexPath.row == 0) {
            CustomerTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomerTitleCell class])];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        CustomerCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomerCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.detailModel.guestList[indexPath.row-1];
        return cell;
    }
    AddCustomerCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AddCustomerCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.addCustomers[indexPath.section];
    return cell;
}
- (void)getNearActivityDetail
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"id"] = @(self.ID);
    [self showClearBgWaiter];
    [ClubNetwork nearActivityDetailWithParame:parame response:^(BOOL success, NSString *msg,ActivityDetailModel *model) {
        [self hideWaiter];
        if (success) {
            self.detailModel = model;
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (void)getNearActivityAddCustomer
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @(self.curPage);
    parame[@"activityId"] = @(self.ID);
    [self showClearBgWaiter];
    [ClubNetwork getNearAdderListWithParame:parame response:^(BOOL success, NSString *msg,NSArray *list) {
        [self hideWaiter];
        if (success) {
            self.addTableView.mj_footer.hidden = list.count<20;
            if (self.curPage == 1&&list.count == 0) {
                [self showNoDataLab:self.addTableView range:CGRectMake(0, ScreenH*0.5-60, ScreenW, 20) title:@"暂无数据"];
                return ;
            }
            [self.addCustomers addObjectsFromArray:list];
            [self.addTableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (void)nearActivityreport
{
    if (self.nameField.text.length == 0) {
        [MBProgressHUD showError:@"请输入姓名"];
        return;
    }
    if (![self.tellField.text isValidMobileNumber]) {
        [MBProgressHUD showError:@"请输入正确的电话号码"];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"activityId"] = @(self.ID);
    parame[@"registrationName"] = self.nameField.text;
    parame[@"registrationPhone"] = self.tellField.text;
    [self showClearBgWaiter];
    [ClubNetwork nearActivityReportWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"报名成功"];
            self.reportView.hidden = YES;
        }else {
            [MBProgressHUD showError:msg];
        }
        self.reportBtn.enabled = NO;
        self.reportBtn.backgroundColor = [UIColor grayColor];
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-64-50) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.estimatedRowHeight = 100;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ActivityDetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ActivityDetailCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ActivityFlowContentCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ActivityFlowContentCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CustomerTitleCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([CustomerTitleCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CustomerCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([CustomerCell class])];
    }
    return _tableView;
}
- (UITableView *)addTableView
{
    if (!_addTableView) {
        _addTableView = [[UITableView alloc]initWithFrame:CGRectMake(ScreenW, 0, ScreenW,ScreenH-64-50) style:UITableViewStyleGrouped];
        _addTableView.dataSource = self;
        _addTableView.delegate = self;
        _addTableView.estimatedRowHeight = 100;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _addTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.curPage++;
            [self getNearActivityAddCustomer];
        }];
        _addTableView.tableFooterView = [[UIView alloc]init];
        [_addTableView registerNib:[UINib nibWithNibName:NSStringFromClass([AddCustomerCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([AddCustomerCell class])];
    }
    return _addTableView;
}
- (NSMutableArray *)addCustomers
{
    if (!_addCustomers) {
        _addCustomers = [NSMutableArray array];
    }
    return _addCustomers;
}

@end
